<?php

/*
=====================================================
 Generic Toolbox - by Carl Wells
-----------------------------------------------------
-----------------------------------------------------
 Copyright (c) 2009 Carl Wells
=====================================================
 THIS IS COPYRIGHTED SOFTWARE
 PLEASE READ THE LICENSE AGREEMENT
=====================================================
 File: system/display/adminpromodisplay.class.php
-----------------------------------------------------
 Purpose: This file builds all visual aspects of the Admin Section.
 All browser output is managed by this file.
=====================================================
*/
require_once("display/genericdisplay.class.php");
require_once("display/admindisplay.class.php");
class Adminposdisplay extends Admindisplay
{

var $currentPOSArray;
             
      function displayPosFrontPage(){
      
		$html = "";
		
		$this->displayCurrentPos();
		
		$html .= "<a href=\"index.php?act=pos\">Current POS</a><br/>\n";
		$html .= "<a href=\"index.php?act=addpos\">Add POS</a><br/>\n";
		
      
		echo($html);
      }
      
      function getCurrentPos(){
      	require_once("core/sector.class.php");
      	require_once("db/dbPos.class.php");
      	$SECTOR = new Sector();
		$array = $SECTOR->getSectors($_GET['sector']);
      	$DB = new DbPos();
      	$DB->connectToServer();
      	$result = $DB->selectQuery("SELECT * FROM `posItems`;");
      	return $result;
      }
      
      function displayCurrentPos(){
      	$result = $this->getCurrentPos();
      	$html = "<h2 class=\"adminheader\">Current POS - <a href=\"index.php?act=addpos\">Add POS</a></h2>";
      	$html .= "<div class=\"catSection\">";
      	while($array = mysql_fetch_array($result)){
      		$html .= "<div class = \"catPromoSection posSection\" >";
			$html .= "<p class=\"adminLink\">".$array['itemName'];
			$html .= " | <a href=\"amendpos.php?id=".$array['id']."\">Amend POS <img src=\"../images/edit_blue_item.gif\" alt=\"Edit Category\" /></a>";
			$html .= " | <a href=\"amendpos.php?id=".$array['id']."&act=del\"  onclick=\"return confirmLink(this, 'delete Breakfast Generic from the system.')\">Delete POS <img src=\"../images/delete_blue_item.gif\" alt=\"Delete POS\" /></a></p>\n";
      		$html .="</div>";
      	}
      	$html .= "</div>";
      	echo $html;
      }
      
      
      
      function displayCurrentPosForForm(){
            		
      	require_once("core/sector.class.php");
      	require_once("db/dbPos.class.php");
      	require_once("functions/stringFunctions.php");
      	$SFUNCTIONS = new StringFunctions();
      	$html ="";
      	$SECTOR = new Sector();
		$array = $SECTOR->getSectors($_GET['sector']);
      	$DB = new DbPos();
      	$DB->connectToServer();
      	$result = $DB->selectQuery("SELECT * FROM `posItems`;");
      	
      	$html .= "<fieldset><legend>Choose POS Items</legend>";
      	$html .= "<table id=\"pos_form\">\n";
      	$html .= "<tr><th>ITEM</th><th>Selected</th><th>Price</th><th>FREE</th><th>Extra Info</th></tr>\n";
      	while($array = mysql_fetch_array($result)){
      	$html .= "<tr>\n";
      	$html .= "<td><label for=\"".$SFUNCTIONS->formFormatted($array['itemName'])."\" class=\"formDescription\">".$array['itemName']."</label></td>";
      	$html .= "<td><input type=\"checkbox\" id=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" checked=\"checked\" /></td>\n";
		$html .= "<td>&pound; <input type=\"text\" id=\"price_".$SFUNCTIONS->formFormatted($array['itemName'])."\" name=\"price_".$SFUNCTIONS->formFormatted($array['itemName'])."\" value=\"".$SFUNCTIONS->formatPrice($array['defaultPrice'])."\" size=\"6\"/></td>\n";
		$html .= "<td><input type=\"checkbox\" id=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\"  /></td>\n";
		$html .= "<td class=\"last_cell\"><input type=\"text\" id=\"extra_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"extra_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"\" size=\"30\" /></td>\n";
		$html .= "</tr>\n";
		//$html .= "<label for=\"".$array['itemName']."\" class=\"formDescription\">".$array['itemName']."<input type=\"checkbox\" id=\"".$array['itemName']."\" name=\"".$array['itemName']."\" value=\"".$array['itemName']." checked=\"checked\" \"><input type=\"text\" id=\"price\" name=\"price\" value=\"price\" size=\"6\"></label><br/>\n";
      	}
      	$html .= "</table>\n";
      	$html .= "</fieldset>";
      	return $html;
      }
      
      function displayCurrentPosEditForm($promo){
            		
      	require_once("core/sector.class.php");
      	require_once("db/dbPos.class.php");
      	require_once("functions/stringFunctions.php");
      	require_once("core/promotionalItem.class.php");
      	$SFUNCTIONS = new StringFunctions();
      	$html ="";
      	$SECTOR = new Sector();
		$array = $SECTOR->getSectors($promo->sector);
		//var_dump($promo);
      	$DB = new DbPos();
      	$DB->connectToServer();
      	// select all the possible promotional items available
      	$result = $DB->selectQuery("SELECT * FROM `posItems`;");
      	
      	
      	// the existing promotional items in the promotion
      	$promos = unserialize( $promo->promotionalItems );
      	$html .= "<div id=\"promoBasicInfo\" style=\"position:relative;\">";
		// BUILD THE EXTRA FUNCTIONS SECTION
		$html .= "<fieldset><legend>Extra Items</legend>";

		$sql = "SELECT * FROM `extraFunctions`";
		$extrasresult = $DB->selectQuery("SELECT * FROM `extraFunctions`");
		while($array = mysql_fetch_array($extrasresult)){
		
		$extras = unserialize($promo->extras);
		//print_r ($extras);
		
		$extraSelected = false;
      	foreach ($promos as $key => $val) {
      	//echo ($array['functionDisplayName']);
      		if ( array_key_exists($array['nameOfFunction'], $extras) ){
      		$extraSelected = true;
      		//echo ($array['functionDisplayName']);
      		}
      	}
      	if($extraSelected){
      		$selected = " checked=\"checked\"";
      	}else{
      		$selected = "";
      	}
		//echo $array['functionDisplayName'];
		$html .= "<div class=\"extraSection\">\n";
		$html .= "<p class=\"formSelection\"><label for=\"".$array['nameOfFunction']."\">".$array['functionDisplayName']."</label><input name=\"".$array['nameOfFunction']."\" id=\"".$array['nameOfFunction']."\" type=\"checkbox\" ".$selected."/></p>\n";
		$html .= "<p class=\"formDescription\">".$array['description']."</p><br/>\n";
		$html .= "</div>\n";
		}
		$html .= "</fieldset>\n";
		$html .= "</div>\n";
      	
      	
      	$html .= "<fieldset><legend>Choose POS Items</legend>";
      	$html .= "<table id=\"pos_form\">\n";
      	$html .= "<tr><th>ITEM</th><th>Selected</th><th>Price</th><th>FREE</th><th>Extra Info</th></tr>\n";
      	
      	// JUST FOR TESTING
      		/*foreach ($promos as $key => $val) {
      		$object = $val;
      		print_r ( $object );
			}*/
			//print_r ( $promo );
      	///////////////////
      	
      	while($array = mysql_fetch_array($result)){
      	$html .= "<tr>\n";
      	$html .= "<td><label for=\"".$SFUNCTIONS->formFormatted($array['itemName'])."\" class=\"formDescription\">".$array['itemName']."</label></td>";
      	
      	
      	// Loop through the objects to see if its there
      	$checkboxSelected = false;
      	foreach ($promos as $key => $val) {
      		if ( strtolower($array['itemName']) == strtolower($val->getItemName()) ){
      		$checkboxSelected = true;
      		}
      	}
      	
      	if($checkboxSelected){
      		$selected = " checked=\"checked\"";
      	}else{
      		$selected = "";
      	}
      	$html .= "<td><input type=\"checkbox\" id=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" ".$selected." /></td>\n";
		
		
		$html .= "<td>&pound; <input type=\"text\" id=\"price_".$SFUNCTIONS->formFormatted($array['itemName'])."\" name=\"price_".$SFUNCTIONS->formFormatted($array['itemName'])."\" value=\"".$SFUNCTIONS->formatPrice($array['defaultPrice'])."\" size=\"6\" /></td>\n";
		$html .= "<td><input type=\"checkbox\" id=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"free_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\"  /></td>\n";
		$html .= "<td class=\"last_cell\"><input type=\"text\" id=\"extra_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" name=\"extra_".$SFUNCTIONS->formFormatted( $array['itemName'] )."\" value=\"\" size=\"30\"></td>\n";
		$html .= "</tr>\n";
		//$html .= "<label for=\"".$array['itemName']."\" class=\"formDescription\">".$array['itemName']."<input type=\"checkbox\" id=\"".$array['itemName']."\" name=\"".$array['itemName']."\" value=\"".$array['itemName']." checked=\"checked\" \"><input type=\"text\" id=\"price\" name=\"price\" value=\"price\" size=\"6\"></label><br/>\n";
      	}
      	$html .= "</table>\n";
      	$html .= "</fieldset>";
      	return $html;
      }
      
      function addPromotion(){
      	require_once("core/sector.class.php");
      	require_once("db/dbconnect.class.php");
      	$SECTOR = new Sector();
		$array = $SECTOR->getSectors($_GET['sector']);
      	$DB = new Connection();
      	$DB->connectToServer();
      	
		$html = "<form>";
		$html .= "<div id=\"promoBasicInfo\" style=\"position:relative;\">";
		$html .= "<p>";
        $html .= "<label for=\"nameOfPromotion\" class=\'promotionalNameLabel\">Name of Promotion</label>";
        $html .= "<input type=\"text\" name=\"nameOfPromotion\" id=\"nameOfPromotion\" />";
		$html .= "</p>";
		// BUILD THE EXTRA FUNCTIONS SECTION
		$html .= "<fieldset>";

      	$sql = "SELECT * FROM `extraFunctions`";
      	$result = $DB->selectQuery("SELECT * FROM `extraFunctions`");
      	while($array = mysql_fetch_array($result)){
      		//echo $array['functionDisplayName'];
      		$html .= "<div class=\"extraSection\">\n";
      		$html .= "<p class=\"formSelection\"><label for=\"".$array['nameOfFunction']."\">".$array['functionDisplayName']."</label><input name=\"".$array['nameOfFunction']."\" id=\"".$array['nameOfFunction']."\" type=\"checkbox\"/></p>\n";
			$html .= "<p class=\"formDescription\">".$array['description']."</p><br>\n";
			$html .= "</div>\n";
      	}
      
		$html .= "</fieldset>\n";
		$html .= "</div>\n";
		$html .= "<div id=\"posItems\">\n";
		
		$html .= "</div>\n";
		$html .= "</form>";
		echo($html);
      }
      
      function displaySectorsInOption(){
      require_once("core/sector.class.php");
      $SECTOR = new Sector();
      $array = $SECTOR->getSectors();
      $html = $this->buildSelectInput($array, 'id', 'nameOfSector');
      return $html;
      }
      
      function displayAddPosFrontPage(){
      $html = "<div class=\"form-container\">";
      $html .= "<form action=\"addpositem.php\" method=\"post\">";
      $html .= "<fieldset><legend>Choose POS Items</legend>";
      $html .= "<table id=\"\">\n";
      // itemName 	dimensions 	description 	notes 	defaultPrice 	type
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"itemName\" class=\'promotionalNameLabel\">Item Name</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<input type=\"text\" name=\"itemName\" id=\"itemName\" />";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">eg. Banner Stand Graphic</p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"dimensions\" class=\'promotionalNameLabel\">Dimensions</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<input type=\"text\" name=\"dimensions\" id=\"dimensions\" />";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">eg. 210mm x 297mm</p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"description\" class=\'promotionalNameLabel\">Description</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<textarea id=\"description\" name=\"description\" rows=\"10\" cols=\"19\">";
	  $html .= "</textarea>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">Enter a brief description of the item here. This may be displayed to the user.</p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"notes\" class=\'promotionalNameLabel\">Notes</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<textarea id=\"notes\" name=\"notes\" rows=\"10\" cols=\"19\">";
	  $html .= "</textarea>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">Enter any notes regarding the item here. This is for internal use only.</p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"defaultPrice\" class=\'promotionalNameLabel\">Default Price</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<input type=\"text\" name=\"defaultPrice\" id=\"defaultPrice\" />";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">eg. 2.99 <span style=\"font-weight:bold;\">NB. do not use the &pound; symbol</span></p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "<tr>";
      $html .= "<td>";
      $html .= "<label for=\"type\" class=\'promotionalNameLabel\">Type</label>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<select id=\"type\" name=\"type\">";
	  $html .= "<option label=\"Banners\" value=\"Banners\">Banners</option>";
	  $html .= "<option label=\"Posters\" value=\"Posters\">Posters</option>";
	  $html .= "<option label=\"POS\" value=\"POS\">POS</option>";
	  $html .= "<option label=\"Table Talkers\" value=\"Table Talkers\">Table Talkers</option>";
	  $html .= "<option label=\"Miscellaneous\" value=\"Miscellaneous\">Miscellaneous</option>";
	  $html .= "</select>";
      $html .= "</td>";
      $html .= "<td>";
      $html .= "<p class=\"formDescription\">Select the type of POS that the item belongs in</p>";
      $html .= "</td>";
      $html .= "</tr>";
      
      $html .= "</table>";
      $html .= "</fieldset>";
      
      $html .= "<div id=\"submitsection\">";
	  $html .= "<button id=\"Submit\" value=\"submit\" type=\"submit\">Submit</button>";
	  $html .= "</div>";
      $html .= "</form>";
       $html .= "</div>";
      	echo ( $html );
      }

	function displayPosEditForm($promo) {
	
		// This is another desperate thing that has gone unchecked for too long
		require_once "config/config.php";
		require_once "db/dbconnect.class.php";
		
		// Create our class instantiations
		$db = new Connection();
		
		// Create our empty variables
		$html = "";
		$sql = "";
		
		// Retreieve the data we need to input into the boxes
		$sql .= "SELECT * FROM `posItems` WHERE `id` = '".$promo."'";
		$result = mysql_query($sql) or die (mysql_error());
		
		$data = mysql_fetch_array($result);
		
		$html .= "<fieldset>";
		$html .= "<legend><b>Edit POS</b></legend>";
		
			// Creating a step system so we can see what's going in where
			$html .= "<table>";
		
				// Start up our function proper, this function is going to post to the same page
				$html .= "<form method=\"post\">\n";
				$html .= "<tr><td style=\"font-size: 11px;\">Item Name </td><td><input type=\"text\" name=\"itemName\" value=\"".$data['itemName']."\" /></td></tr>\n";
				$html .= "<tr><td style=\"font-size: 11px;\">Dimensions </td><td><input type=\"text\" name=\"dimensions\" value=\"".$data['dimensions']."\" /></td></tr>\n";
				$html .= "<tr><td style=\"font-size: 11px;\">Description </td><td><textarea id=\"description\" name=\"description\" rows=\"10\" cols=\"19\">".$data['description']."</textarea>";
				$html .= "<tr><td style=\"font-size: 11px;\">Notes </td><td><textarea id=\"description\" name=\"notes\" rows=\"10\" cols=\"19\">".$data['notes']."</textarea>";
				$html .= "<tr><td style=\"font-size: 11px;\">Default Price </td><td><input type=\"text\" name=\"defaultPrice\" value=\"".$data['defaultPrice']."\" /></td></tr>\n";
				$html .= "<tr><td style=\"font-size: 11px;\">Type </td><td>";
				$html .= "<select id=\"type\" name=\"type\">";
					// Select box begins
					$html .= "<option label=\"Banners\" value=\"Banners\">Banners</option>";
					$html .= "<option label=\"Posters\" value=\"Posters\">Posters</option>";
					$html .= "<option label=\"POS\" value=\"POS\">POS</option>";
					$html .= "<option label=\"Table Talkers\" value=\"Table Talkers\">Table Talkers</option>";
					$html .= "<option label=\"Miscellaneous\" value=\"Miscellaneous\">Miscellaneous</option>";
					$html .= "</select></td><td><p class=\"formDescription\">Select the type of POS that the item belongs in</p></td></tr>";
					
				// And finally our submit button
				$html .= "<tr><td><input type=\"submit\" name=\"submit\" value=\"Edit\" /></td></tr>";
				
			$html .= "</table>";
			
		$html .= "</fieldset>";
		
		return $html;
	
	}
	
	function editPosItem($promo, $edit) {
	
		// Let's finish this baby off
		require_once "config/config.php";
		require_once "db/dbconnect.class.php";
		
		// Instantiate our class
		$db = new Connection();
		
		// Create our empty variables
		$sql = "";
		
		// Start off our sql statement
		$sql .= "UPDATE `posItems` SET ";
		
			// Run through the post to create our sql statement
			foreach ($edit as $key => $val) {
				
				// If the key is submit, we don't want to include it
				if ($key != "submit") {
				
					$sql .= $key." = '".$val."', ";
				
				}
			
			}
			
		// Remove the last comma
		$sql = substr_replace($sql, "", -2);
		
		// And end it with the where statement
		$sql .= " WHERE `id` = '".$promo."'";
			
		mysql_query($sql) or die (mysql_error);
		
		// Create a success session
		session_start();
		$_SESSION['success'] = "Your changes have been written successfully";
	
	}
	
	function deletePosItem($pos) {
	
		// Include our files that we need
		require_once "config/config.php";
		require_once "db/dbconnect.class.php";
		
		// Instantiate our class
		$db = new Connection();
		
		// Create our very quick SQL statement
		$sql = "DELETE FROM `posItems` WHERE `id` = '".$pos."'";
		
		mysql_query($sql) or die (mysql_error());
	
	}

}

?>